/*
 * @Author: 一路向阳 tt_sunzhenfeng@163.com
 * @Date: 2024-05-16 20:31:50
 * @LastEditors: 一路向阳 tt_sunzhenfeng@163.com
 * @LastEditTime: 2024-05-17 22:40:09
 * @FilePath: \webpack5_demo\webpack.config.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin=require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const ESlintPlugin = require('eslint-webpack-plugin');


module.exports = {
  // 打包模式配置
  mode: 'development',
  // ⼊⼝⽂件配置
  entry: './src/index.js',
  // 出⼝⽂件配置
  output: {
    // 所有输出⽂件的⽬标路径，必须是绝对路径
    path: resolve(__dirname, './dist'),
    // 出⼝⽂件名配置
    filename: "bundle.js",
  },
  // 模块解析
  resolve: {
    alias: {
      '@': resolve('src'),
    },
    extensions: ['.js', '.json', '.less'],
  },
  // 模块配置
  module: {
    rules: [
      // 配置多个规则
      // 解析css
      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
      },

      // 解析less
      {
        test: /\.less$/i,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
          'less-loader',
        ],
      },

      // 编辑es5以上的语法
      {
        test: /\.m?js$/i,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              [
                '@babel/preset-env',
                {
                  useBuiltIns: 'usage', // 按需加载
                  corejs: 3,
                  targets: 'defaults',
                },
              ],
            ],
          },
        },
      },

      // 解析打包图片
      {
        test: /\.(png|gif|jpe?g)$/i,
        type: 'asset',
        parser: {
          dataUrlCondition: {
            maxSize: 8 * 1024,
          },
        },
        generator: {
          filename: './images/[name][ext]',
        },
      },

      // 解析打包字体
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/i,
        // asset可以在asset/inline和asset/resource之间进行切换，文件小于8kb时使用asset/inline，否则使用asset/resource
        type: 'asset',
        parser: {
          dataUrlCondition: {
            maxSize: 8 * 1024,
          },
        },
        generator: {
          filename: './fonts/[name][ext]',
        },
      },
    ],
  },
  // 开发服务器
  devServer: {
    static: {
      directory: resolve(__dirname, 'output'),
    },
    liveReload: true,
    compress: true,
    port: 8888,
    proxy: {
      '/api': {
        target: 'https://api.xdclass.net',
        pathRewrite: {
          '^/api': '',
        },
        secure: false,
      },
      changeOrigin: true,
    },
  },

  target: 'web',
  // 插件配置
  plugins: [
    // 自动生成HTML文件和指定的模板
    new HtmlWebpackPlugin({
      template:'./src/index.html', //指定 html 模板
      filename:'index.html', //指定 html 名称
      title:'hello webpack', //指定 html title
      minify: {
        collapseWhitespace: true, // 清除空格换⾏
        removeComments: true, // 清除注释
      },
    }),
    // 抽离css代码生成独立的文件
    new MiniCssExtractPlugin({
      filename:'./css/main.css'
    }),
    // 压缩css
    new OptimizeCssAssetsPlugin(),
    // 引用eslint代码校验
    new ESlintPlugin({
      fix: true // ⾃动修正不符合规范的代码
    })
  ]
};